package io.xccit.hotelsystem.ui.hotel;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.ui.login.LoginActivity;
import io.xccit.hotelsystem.utils.AuthUtils;
import io.xccit.hotelsystem.utils.SQLiteHelper;

/**
 * 酒店详情页
 */
public class HotelDetailActivity extends AppCompatActivity {
    private int hotelId;
    private String hotelName;
    private String hotelPrice;
    private int hotelImage;
    private String hotelDesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotel_detail);
        
        Intent intent = getIntent();
        hotelId = intent.getIntExtra("hotel_id", -1);
        hotelName = intent.getStringExtra("hotel_name");
        hotelPrice = intent.getStringExtra("hotel_price");
        hotelImage = intent.getIntExtra("hotel_image", R.drawable.hotel_home_1);
        hotelDesc = intent.getStringExtra("hotel_desc");
        
        ImageView imageView = findViewById(R.id.ivHotel);
        TextView nameView = findViewById(R.id.tvHotelName);
        TextView descView = findViewById(R.id.tvHotelDesc);
        TextView priceView = findViewById(R.id.tvHotelPrice);
        Button bookButton = findViewById(R.id.btnBook);
        
        Glide.with(this).load(hotelImage).into(imageView);
        nameView.setText(hotelName);
        if (hotelDesc != null && !hotelDesc.isEmpty()) {
            descView.setText(hotelDesc);
        }
        priceView.setText("¥" + hotelPrice);
        priceView.setTextColor(Color.parseColor("#FF4400"));
        
        bookButton.setOnClickListener(v -> showBookingDialog());
    }
    
    private void showBookingDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_booking, null);
        
        EditText etName = dialogView.findViewById(R.id.etName);
        EditText etPhone = dialogView.findViewById(R.id.etPhone);
        TextView tvCheckIn = dialogView.findViewById(R.id.tvCheckIn);
        TextView tvCheckOut = dialogView.findViewById(R.id.tvCheckOut);
        
        tvCheckIn.setOnClickListener(v -> showDatePickerDialog(v));
        tvCheckOut.setOnClickListener(v -> showDatePickerDialog(v));
        
        builder.setView(dialogView)
            .setTitle("预定酒店")
            .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
            .setPositiveButton("确认", (dialog, which) -> {
                if (!AuthUtils.isLoggedIn(this)) {
                    Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                
                String name = etName.getText().toString().trim();
                String phone = etPhone.getText().toString().trim();
                String checkIn = tvCheckIn.getText().toString().trim();
                String checkOut = tvCheckOut.getText().toString().trim();
                
                if (name.isEmpty()) {
                    Toast.makeText(this, "请输入预定人姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (phone.isEmpty()) {
                    Toast.makeText(this, "请输入联系电话", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (checkIn.equals("选择日期")) {
                    Toast.makeText(this, "请选择入住时间", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (checkOut.equals("选择日期")) {
                    Toast.makeText(this, "请选择离店时间", Toast.LENGTH_SHORT).show();
                    return;
                }
                
                SQLiteHelper dbHelper = new SQLiteHelper(this);
                SharedPreferences prefs = getSharedPreferences(AuthUtils.PREF_NAME, Context.MODE_PRIVATE);
                String username = prefs.getString(AuthUtils.KEY_USERNAME, "");
                dbHelper.addOrder(username, hotelName, checkIn, checkOut, 1, Double.parseDouble(hotelPrice), String.valueOf(hotelImage), name, phone);
                Toast.makeText(this, "预定成功", Toast.LENGTH_SHORT).show();
            });
        
        builder.create().show();
    }

    /**
     * 日期选择器组件
     * @param view
     */
    private void showDatePickerDialog(View view) {
        TextView textView = (TextView) view;
        LocalDate now = LocalDate.now();
        new android.app.DatePickerDialog(this, (picker, year, month, day) -> {
            String date = year + "-" + ( month + 1 ) + "-" + day;
            textView.setText(date);
        }, now.getYear(), (now.getMonth().getValue()) - 1, now.getDayOfMonth()).show();
    }
}